package com.bootdo.program.wallpaper.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.config.Constant;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.program.wallpaper.domain.BzConfigDO;
import com.bootdo.program.wallpaper.service.BzConfigService;
import com.bootdo.program.weixin.domain.WxAdvertDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.program.wallpaper.domain.BzBaseinfoDO;
import com.bootdo.program.wallpaper.service.BzBaseinfoService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 小程序基础配置
 * 
 * @author guoyunfei
 * @email 2549789059@qq.com
 * @date 2021-08-12 21:59:33
 */
 
@Controller
@RequestMapping("/wallpaper/bzBaseinfo")
public class BzBaseinfoController extends BaseController {
	@Autowired
	private BzBaseinfoService bzBaseinfoService;

	@Autowired
	private BzConfigService bzConfigService;
	
	@GetMapping()
	@RequiresPermissions("wallpaper:bzBaseinfo:bzBaseinfo")
	String BzBaseinfo(Model model){
		List<BzConfigDO> platform_type = bzConfigService.getType("platform_type");
		model.addAttribute("platform_type",platform_type);
	    return "wallpaper/bzBaseinfo/bzBaseinfo";
	}


	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wallpaper:bzBaseinfo:bzBaseinfo")
	public PageUtils list(@RequestParam Map<String, Object> params){
		List<BzConfigDO> platformTypes = bzConfigService.getType("platform_type");
		//查询列表数据
		Query query = new Query(params);
		List<BzBaseinfoDO> list = bzBaseinfoService.list(query);
		for (int i = 0; i < list.size(); i++) {
			for (int i1 = 0; i1 < platformTypes.size(); i1++) {
				if(list.get(i).getPlatformType() == Integer.valueOf(platformTypes.get(i1).getValue()).intValue()){
					list.get(i).setPlatformTypeVal(platformTypes.get(i1).getLabel());
					break;
				}
			}
		}
		int total = bzBaseinfoService.count(query);
		PageUtils pageUtils = new PageUtils(list, total);
		return pageUtils;
	}


	@GetMapping("form/{id}")
	@RequiresPermissions("wallpaper:bzBaseinfo:edit")
	String BzBaseinfoForm(@PathVariable("id") Integer id,Model model){
		model.addAttribute("id",id);
		List<BzConfigDO> platform_type = bzConfigService.getType("platform_type");
		model.addAttribute("platform_type",platform_type);
		return "wallpaper/bzBaseinfo/bzBaseinfoForm";
	}

	@GetMapping("/get/{id}")
	@ResponseBody
	R get (@PathVariable Integer id){
		BzBaseinfoDO aDo = bzBaseinfoService.get(id);
		Map<String,Object> map = new HashMap<>();
		map.put("baseinfoDO",aDo);
		return R.ok(map);
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wallpaper:bzBaseinfo:edit")
	public R update( BzBaseinfoDO bzBaseinfo){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		BzBaseinfoDO baseinfoDO = bzBaseinfoService.getByAppid(bzBaseinfo.getAppid());
		if(null == bzBaseinfo.getId()){

			if(null != baseinfoDO){
				return R.error("appid不允许重复");
			}
			bzBaseinfoService.save(bzBaseinfo);
			return R.ok();
		}

		bzBaseinfoService.update(bzBaseinfo);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wallpaper:bzBaseinfo:remove")
	public R remove( Integer id){
		if(bzBaseinfoService.remove(id)>0){
			return R.ok();
		}
		return R.error();
	}

}
